package com.ocom.srvpaccess.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.dto.MachInfoDto;
import com.ocom.common.entity.access.EntityMjDeviceInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.srvpaccess.dao.IDaoMjDeviceInfo;
import com.ocom.srvpaccess.service.MjDeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class MjDeviceInfoServiceImpl extends ServiceImpl<IDaoMjDeviceInfo, EntityMjDeviceInfo> implements MjDeviceInfoService {



    @Autowired
    private IDaoMjDeviceInfo mjDeviceInfo;

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjDeviceInfo getDeviceInfo(Long companyId, String machNo) {
        QueryWrapper<EntityMjDeviceInfo> query = Wrappers.query();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        if (StringUtils.isNotEmpty(machNo)){
            query.eq("device_no",machNo);
        }
        query.last("limit 1");
        return mjDeviceInfo.selectOne(query);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public MachInfoDto selectmachNo(Long companyId, String machNo) {
        return mjDeviceInfo.selectmachNo(companyId,machNo);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateAppVer(Long companyId, String machNo, String versionCode) {

        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        query.eq("com_id",companyId).eq("device_no",machNo);
        query.set("app_ver",versionCode);
        this.update(query);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void changeMachParaState(Long companyId, String machNo, String jobId) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        if (StringUtils.isNotEmpty(machNo)){
            query.eq("device_no",machNo);
        }
        if (StringUtils.isNotEmpty(jobId)){
            query.eq("para_jobid",jobId);
        }

        EntityMjDeviceInfo entityMjDeviceInfo = new EntityMjDeviceInfo();
        entityMjDeviceInfo.setParaState(0L);
        mjDeviceInfo.update(entityMjDeviceInfo,query);
    }
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateZoneVerLast(Long companyId,String machNo, String zoneVer) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        if (StringUtils.isNotEmpty(machNo)){
            query.eq("device_no",machNo);
        }
        EntityMjDeviceInfo entityMjDeviceInfo = new EntityMjDeviceInfo();
        entityMjDeviceInfo.setZoneVerLast(zoneVer);
        mjDeviceInfo.update(entityMjDeviceInfo,query);
    }

    @Override
    public void updateZoneVerTab(Long companyId, String machNo, String zoneVerLast) {
        // zone_ver_tab = zone_ver_last
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        if (StringUtils.isNotEmpty(machNo)){
            query.eq("device_no",machNo);
        }
        EntityMjDeviceInfo entityMjDeviceInfo = new EntityMjDeviceInfo();
        entityMjDeviceInfo.setZoneVerTab(zoneVerLast);

        mjDeviceInfo.update(entityMjDeviceInfo,query);
    }
    /**
     * 1、修改门禁设备表mj_device_info中com_id、device_no匹配的记录：
     * */
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateRsaState(Long companyId, String machNo, String jobId) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        if (StringUtils.isNotEmpty(machNo)){
            query.eq("device_no",machNo);
        }
        if (StringUtils.isNotEmpty(jobId)){
            query.eq("rsa_state",jobId);
        }
        EntityMjDeviceInfo entityMjDeviceInfo = new EntityMjDeviceInfo();
        entityMjDeviceInfo.setRsaState(0L);
        mjDeviceInfo.update(entityMjDeviceInfo,query);

    }
    /**
     * 2、如果perGroupNum=0：mj_device_info . group_var_last = mj_device_info . group_ver
     * */
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateGroupVarLast(Long companyId, String machNo, String groupVer) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        if (StringUtils.isNotEmpty(machNo)){
            query.eq("device_no",machNo);
        }
        if (StringUtils.isNotNull(groupVer)){
            query.eq("group_ver",groupVer);
        }
        EntityMjDeviceInfo entityMjDeviceInfo = new EntityMjDeviceInfo();
        entityMjDeviceInfo.setGroupVerLast(groupVer);
        mjDeviceInfo.update(entityMjDeviceInfo,query);
    }

    // 如果没有记录：mj_device_info . group_var_tab = mj_device_info . group_ver_last，返回NULL
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateGroupVarTab(Long companyId, String machNo, String groupVerLast) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        if (StringUtils.isNotEmpty(machNo)){
            query.eq("device_no",machNo);
        }
        if (StringUtils.isNotNull(groupVerLast)){
            query.eq("group_ver_last",groupVerLast);
        }
        EntityMjDeviceInfo entityMjDeviceInfo = new EntityMjDeviceInfo();
        entityMjDeviceInfo.setGroupVerTab(groupVerLast);
        mjDeviceInfo.update(entityMjDeviceInfo,query);
    }


    //1从设备信息表mj_device_info找到com_id ，device_no匹配的记录得到areaNo。
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjDeviceInfo selectAreaNo(Long companyId, String machNo) {
        QueryWrapper<EntityMjDeviceInfo> query = Wrappers.query();
        query.eq("com_id",companyId).eq("device_no",machNo).last("limit 1");
        return this.getOne(query);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateDeviceInfo(Long companyId,String machNo, Long listAddTab, Long listDeleteTab, Long maxId) {
        UpdateWrapper<EntityMjDeviceInfo> update = Wrappers.update();
        update.eq("com_id",companyId);
        update.eq("device_no",machNo);
        update.set("list_add_tab",listAddTab);
        update.set("list_delete_tab",listDeleteTab);
        if (StringUtils.isNotNull(maxId)){
            update.set("list_delete",maxId);
        }
        this.update(update);
    }


    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateListDel(Long companyId,String machNo, Long listAddTab, Long maxId) {
        UpdateWrapper<EntityMjDeviceInfo> update = Wrappers.update();
        update.eq("com_id",companyId);
        update.eq("device_no",machNo);
        update.set("list_add_tab",listAddTab);
        update.set("list_delete_tab",maxId);
        this.update(update);
    }
    //update mj_device_info set list_delete = list_delete_tab where device_id=[deviceId]
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateListDelDeviceId(Long companyId, Long deviceId, Long listDeleteTab) {
        UpdateWrapper<EntityMjDeviceInfo> update = Wrappers.update();
        update.eq("com_id",companyId);
        update.eq("device_id",deviceId);
        update.set("list_delete",listDeleteTab);
        this.update(update);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateListAdd(Long companyId,String machNo, Long listDeleteTab, Long maxId) {
        UpdateWrapper<EntityMjDeviceInfo> update = Wrappers.update();
        update.eq("com_id",companyId);
        update.eq("device_no",machNo);
        update.set("list_delete_tab",listDeleteTab);
        update.set("list_add_tab",maxId);
        this.update(update);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateListAddDeviceId(Long companyId, Long listAddTab,Long deviceId) {
        UpdateWrapper<EntityMjDeviceInfo> update = Wrappers.update();
        update.eq("com_id",companyId);
        update.eq("device_id",deviceId);
        update.set("list_add",listAddTab);
        this.update(update);
    }

    //修改mj_device_info表：
    //list_add_tab=-1
    //list_delete_tab=-1
    //list_State=2
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updatePerNo(Long companyId,  String machNo) {
        UpdateWrapper<EntityMjDeviceInfo> update = Wrappers.update();
        update.eq("com_id",companyId);
        update.eq("device_no",machNo);
        update.set("list_delete_tab",-1);
        update.set("list_add_tab",-1);
        update.set("list_state",2);
        this.update(update);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateAppVerLast(Long companyId,String machNo,  String AppVerTab) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        if (StringUtils.isNotEmpty(machNo)){
            query.eq("device_no",machNo);
        }
        if (StringUtils.isNotNull(AppVerTab)){
            query.eq("app_ver_tab",AppVerTab);
        }
        EntityMjDeviceInfo entityMjDeviceInfo = new EntityMjDeviceInfo();
        entityMjDeviceInfo.setAppVerLast(AppVerTab);

        mjDeviceInfo.update(entityMjDeviceInfo,query);
    }

    //修改Jobid数据
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateJobId(Long companyId, String machNo, Long jobId) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        if (StringUtils.isNotEmpty(machNo)){
            query.eq("device_no",machNo);
        }
        EntityMjDeviceInfo entityMjDeviceInfo = new EntityMjDeviceInfo();
        entityMjDeviceInfo.setAppJobid(jobId);

        mjDeviceInfo.update(entityMjDeviceInfo,query);
    }

    //修改 list_State=0
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateListStateTwo(Long companyId, Long deviceId) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        query.eq("com_id",companyId);
        query.eq("device_id",deviceId);
        query.eq("list_state",2);
        query.set("list_state",0);
        this.update(query);
    }
    //修改 list_State=1
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateListStateOne(Long companyId,Long deviceId) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        query.eq("com_id",companyId);
        query.eq("device_id",deviceId);
        query.set("list_state",1);
        this.update(query);
    }

    //修改分库库mj_device_info表：
    //	update mj_device_info
    //set status=0, end_time = EndDate
    //				where com_id= companyId and device_no=machNo
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateEndTime(Long companyId, String machNo) {
        QueryWrapper<EntityMjDeviceInfo> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("device_no",machNo);
        EntityMjDeviceInfo entityMjDeviceList = new EntityMjDeviceInfo();
        entityMjDeviceList.setStatus(0L);
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 3);
        entityMjDeviceList.setEndTime(calendar.getTime());
        mjDeviceInfo.update(entityMjDeviceList,query);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void updateCommTimeCommMode(Long companyId, String machNo, Integer commMode) {
        UpdateWrapper<EntityMjDeviceInfo> query = Wrappers.update();
        query.eq("com_id",companyId);
        query.eq("device_no",machNo);
        EntityMjDeviceInfo entityMjDeviceList = new EntityMjDeviceInfo();
        entityMjDeviceList.setCommTime(new Date());
        query.set("comm_mode",commMode);
        query.set("comm_time",entityMjDeviceList.getCommTime());
        this.update(query);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public int selectMachCount(Long comId, Integer regionId, Integer areaId) {
        QueryWrapper<EntityMjDeviceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", comId);
        queryWrapper.in("device_ver_type", 7,8);
        queryWrapper.eq("status", 2);
        //如果areaId=-1,代表查找片区下设备数
        if (areaId==-1){
            if (regionId==0){
                //如果regionId为0 或 areaId=0，代表查找直接挂靠单位下的设备，则areaId=0
                queryWrapper.eq("area_id", 0);
                return mjDeviceInfo.selectCount(queryWrapper);
            }else {
                //如果regionId>0 ,则先查找片区下的区域areaId，再通过areaId 查找区域下的设备数
                queryWrapper.inSql("area_id", "select area_id FROM mj_area_info where com_id="+comId+" and  region_id = "+regionId);
                return mjDeviceInfo.selectCount(queryWrapper);
            }
        }else {
            queryWrapper.eq("area_id", areaId);
            return  mjDeviceInfo.selectCount(queryWrapper);
        }
    }

    /*
     * @Description: 根据设备号获取片区id
     * @Author: lu 
     * @Date: 2022/4/12 19:12
     * @param companyId: 
     * @param machNo:  
     * @return: int
     **/
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public int selectAreaId(Long companyId, String machNo) {
        QueryWrapper<EntityMjDeviceInfo> wrapper = new QueryWrapper<>();
        wrapper.select("area_id");
        wrapper.eq("com_id",companyId);
        wrapper.eq("device_no",machNo);
        EntityMjDeviceInfo entityMjDeviceInfo=mjDeviceInfo.selectOne(wrapper);
        if (entityMjDeviceInfo!=null){
            if (entityMjDeviceInfo.getAreaId()!=null){
                return Math.toIntExact(entityMjDeviceInfo.getAreaId());
            }
        }
        return 0;
    }


    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<String> getCenterMachByCompanyId(Long companyId) {
        QueryWrapper<EntityMjDeviceInfo> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("device_ver_type",8);

        query.and(wrapper ->wrapper.ge("end_time", new Date()).or().isNull("end_time"));
        query.select("device_no");
        List<EntityMjDeviceInfo> list= this.list(query);
        List<String> list1=new ArrayList<>();
        if(list==null || list.size()==0){
            return list1;
        }
        for (EntityMjDeviceInfo mach : list) {
            list1.add(mach.getDeviceNo());
        }
        return list1;
    }


}
